<template>
  <div class="cart">
    <h4>全部商品</h4>
    <div class="cart-main">
      <div class="cart-th">
        <div class="cart-th1">全部</div>
        <div class="cart-th2">商品</div>
        <div class="cart-th3">规格</div>
        <div class="cart-th4">单价（元）</div>
        <div class="cart-th5">数量</div>
        <div class="cart-th6">小计（元）</div>
        <div class="cart-th7">操作</div>
      </div>
      <div class="cart-body">
        <!--购物车数据-->
        <ul class="cart-list" v-for="good in cartInfoList" :key="good.id">
          <li class="cart-list-con1">
            <!--v-model 如果使用在checkbox上 其实vue最终操作的是这个checkbox的checked属性(通过prop的方式进行操作)-->
            <!--当good.isChecked产生改变  其实vue内部 会进行一次dom操作  node.checked=Boolean(good.isChecked对应的值)-->
            <!--
              vuex仓库中的当前商品的数据有没有变? 变了! 但是不符合咱们vuex的操作规范! 没有通过mutation来进行修改!!
                通过mutation来进行修改
              数据库里当前商品的数据有没有变? 没有变的!!!!!
                发请求 该数据库中的数据!
             -->
            <!--<input type="checkbox" name="chk_list" v-model="good.isChecked">-->
            <input type="checkbox" name="chk_list"
                   :checked="good.isChecked" @change="checkedChangeFn(good.skuId,$event)">
          </li>
          <li class="cart-list-con2">
            <img :src="good.imgUrl">
            <div class="item-msg">{{good.skuName}}</div>
          </li>
          <li class="cart-list-con3">
            <div class="item-txt">语音升级款</div>
          </li>
          <li class="cart-list-con4">
            <span class="price">{{good.skuPrice}}</span>
          </li>
          <li class="cart-list-con5">
            <a href="javascript:void(0)" class="mins"
               @click="updateCount(good.skuNum,good.skuId,-1)">-</a>
            <input autocomplete="off" type="text" value="1"
                   minnum="1" class="itxt" :value="good.skuNum"
                    @change="updateCountByInput(good.skuNum,good.skuId,$event)">
            <a href="javascript:void(0)" class="plus"
               @click="updateCount(good.skuNum,good.skuId,1)">+</a>
          </li>
          <li class="cart-list-con6">
            <span class="sum">{{good.skuPrice * good.skuNum}}</span>
          </li>
          <li class="cart-list-con7">
            <a href="javascript:;" class="sindelet" @click="delCartFn(good.skuId,good.skuName)">删除</a>
            <br>
            <a href="#none">移到收藏</a>
          </li>
        </ul>
      </div>
    </div>
    <div class="cart-tool">
      <div class="select-all">
        <input class="chooseAll" type="checkbox" v-model="checkAll" :disabled="isEmpty">
        <span>全选</span>
      </div>
      <div class="option">
        <a href="javascript:;" @click="deleteCheckedFn">删除选中的商品</a>
        <a href="#none">移到我的关注</a>
        <a href="#none">清除下柜商品</a>
      </div>
      <div class="money-box">
        <div class="chosed">已选择
          <span style="color: green">{{totalNum}}</span>件商品</div>
        <div class="sumprice">
          <em>总价（不含运费） ：</em>
          <i class="summoney" style="color: red">{{totalPrice}}</i>
        </div>
        <div class="sumbtn">
          <router-link class="sum-btn" to="/trade">结算</router-link>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import {mapActions,mapGetters} from "vuex";
  export default {
    name: 'ShopCart',
    computed:{
      ...mapGetters(["cartInfoList","isEmpty","totalNum","totalPrice"]),
      checkAll:{
        get(){
          return this.$store.getters.checkAll;
        },
        async set(checked){
          // checked: 全选按钮的最新状态
          // 进行全选的逻辑!  code为200 代表请求成功  ; 400 代表失败
          await this.checkedAll(checked);
        }
      }
    },
    methods:{
      ...mapActions(["getCartList","checkedChange",
        "checkedAll","deleteCart","deleteChecked",
      "addToCartForShopcart"]),

      //购物车数量的加减操作 通过输入框输入购物车数量
      async updateCountByInput(skuNum,skuId,ev){
        let total = Number(ev.target.value);
        let count = total - Number(skuNum);
        await this.updateCount(skuNum,skuId,count)
      },
      //购物车数量的加减操作
      async updateCount(skuNum,skuId,count){
        //count:增量  skuNum:原始的数量
        //计算总数量 如果总数量小于等于0 则禁止添加
        //total = 原始的数量 + 增量
        let total =skuNum + count;
        if (total<=0){
          this.$message.error("总数量不能小于等于0");
          return;
        }

        //判断一下count是不是NAN
        if(Number.isNaN(count)){
          this.$message.error("输入数量不合法");
          return;
        }

        //total合法调用添加购物车的action
        try {
          const code = await this.addToCartForShopcart({skuId,skuNum:count})
          if(code === 200){
            //添加操作成功  刷新购物车界面
            await this.getCartList()
          }else{
            //添加操作失败
            this.$message.error("购物车添加失败")
          }
        }catch (err){
          //网络问题
          this.$message.error("网络问题")
        }
      },
      //勾选操作
      async checkedChangeFn(skuId,ev){
        //id:当前进行勾选操作的商品
        const checked = ev.target.checked;
        //await 返回值是当前选中是否成功的状态嘛 如果为200代表成功 如果不为200代表失败(400 代表当前请求是一个bad requset)
        await this.checkedChange({skuId,checked});
        await this.getCartList()
      },
      //删除操作(单删)
      async delCartFn(skuId,skuName){
        //弹窗提醒
        await this.$confirm(`是否要删除${skuName}嘛`,{
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
        //调action
        await this.deleteCart(skuId)
      },
      //删除所有选中的
      async deleteCheckedFn(){
        //弹窗提醒
        await this.$confirm(`是否要删除所有的选中商品?`,{
          distinguishCancelAndClose: true,
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        })
        await this.deleteChecked()
      }
    },
    async mounted() {
      await this.getCartList()
    }
  }
</script>

<style lang="less" scoped>
.cart {
  width: 1200px;
  margin: 0 auto;

  h4 {
    margin: 9px 0;
    font-size: 14px;
    line-height: 21px;
  }

  .cart-main {
    .cart-th {
      background: #f5f5f5;
      border: 1px solid #ddd;
      padding: 10px;
      overflow: hidden;

      &>div {
        float: left;
      }

      .cart-th1 {
        width: 7.5%;

        input {
          vertical-align: middle;
        }

        span {
          vertical-align: middle;
        }
      }

      .cart-th2 {
        width: 30%;
      }

      .cart-th3,
      .cart-th4,
      .cart-th5,
      .cart-th6,
      .cart-th7 {
        width: 12.5%;
      }
    }

    .cart-body {
      margin: 15px 0;
      border: 1px solid #ddd;

      .cart-list {
        padding: 10px;
        border-bottom: 1px solid #ddd;
        overflow: hidden;

        &>li {
          float: left;
        }

        .cart-list-con1 {
          width: 7.5%;
        }

        .cart-list-con2 {
          width: 30%;

          img {
            width: 82px;
            height: 82px;
            float: left;
          }

          .item-msg {
            float: left;
            width: 150px;
            margin: 0 10px;
            line-height: 18px;
          }
        }

        .cart-list-con3 {
          width: 12.5%;
        }

        .cart-list-con4 {
          width: 12.5%;
          min-height: 1px;
        }

        .cart-list-con5 {
          width: 12.5%;

          .mins {
            border: 1px solid #ddd;
            border-right: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }

          input {
            border: 1px solid #ddd;
            width: 40px;
            height: 33px;
            float: left;
            text-align: center;
            font-size: 14px;
          }

          .plus {
            border: 1px solid #ddd;
            border-left: 0;
            float: left;
            color: #666;
            width: 6px;
            text-align: center;
            padding: 8px;
          }
        }

        .cart-list-con6 {
          width: 12.5%;

          .sum {
            font-size: 16px;
          }
        }

        .cart-list-con7 {
          width: 12.5%;

          a {
            color: #666;
          }
        }
      }
    }
  }

  .cart-tool {
    overflow: hidden;
    border: 1px solid #ddd;

    .select-all {
      padding: 10px;
      overflow: hidden;
      float: left;

      span {
        vertical-align: middle;
      }

      input {
        vertical-align: middle;
      }
    }

    .option {
      padding: 10px;
      overflow: hidden;
      float: left;

      a {
        float: left;
        padding: 0 10px;
        color: #666;
      }
    }

    .money-box {
      float: right;

      .chosed {
        line-height: 26px;
        float: left;
        padding: 0 10px;
      }

      .sumprice {
        width: 200px;
        line-height: 22px;
        float: left;
        padding: 0 10px;

        .summoney {
          color: #c81623;
          font-size: 16px;
        }
      }

      .sumbtn {
        float: right;

        a {
          display: block;
          position: relative;
          width: 96px;
          height: 52px;
          line-height: 52px;
          color: #fff;
          text-align: center;
          font-size: 18px;
          font-family: "Microsoft YaHei";
          background: #e1251b;
          overflow: hidden;
        }
      }
    }
  }
}
</style>